package com.zhilianbao.leyaogo.ui.adapter.goodscategory;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.bql.baseadapter.recycleView.QuickRcvAdapter;
import com.bql.baseadapter.recycleView.QuickRcvHolder;
import com.bql.convenientlog.CLog;
import com.zhilianbao.leyaogo.R;
import com.zhilianbao.leyaogo.listener.NoDoubleClickListener;
import com.zhilianbao.leyaogo.model.response.category.GoodsInfo;
import com.zhilianbao.leyaogo.utils.ImageUtils;
import com.zhilianbao.leyaogo.utils.Utils;
import com.zhilianbao.leyaogo.view.widgets.MoneyTextView;

import java.util.List;


/**
 * Created by gyt on 2016/10/31
 */
public class GoodsAdapter extends QuickRcvAdapter<GoodsInfo> {
    private final int mClassifyType;
    private OnAddToCartClickListener mOnAddToCartClickListener;

    public GoodsAdapter(Context context, List<GoodsInfo> data, int classifyType) {
        super(context, data, R.layout.item_category_content);
        // 判断该商品是活动商品。类别 0表示商品分类 1表示专题
        mClassifyType = classifyType;
        CLog.i("GoodsAdapter mClassifyType = " + mClassifyType);
    }

    @Override
    protected void bindDataHelper(QuickRcvHolder viewHolder, int position, GoodsInfo item) {
        int goodsSkuId = item.getGoodsSkuId();
        int goodsType = item.getGoodsType();
        viewHolder.setText(R.id.tv_goods_name, item.getGoodsName());
        viewHolder.setText(R.id.tv_goods_desc, item.getGoodsDesc());
        ((MoneyTextView) viewHolder.getView(R.id.tv_goods_price_vip_value)).setAmount(goodsType == 1 && goodsSkuId != 0 ? item.getSkuPrice() : item.getPrice());
        ImageView goodsLogo = (ImageView) viewHolder.getView(R.id.iv_goods_logo);
        ImageUtils.loadGoodsPic(Utils.returnUrlShow(item.getGoodsPic()), goodsLogo);
        viewHolder.getView(R.id.iv_category_add_cart).setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                mOnAddToCartClickListener.onAddToCartClick(goodsLogo, viewHolder, position);
            }
        });
        if (goodsType == 1 && goodsSkuId != 0) {
            String goodsItemVals = item.getItemVals();
            viewHolder.setText(R.id.tv_goods_desc, goodsItemVals);
        }
        // 显示标签
        if ((mClassifyType == 0 && item.getActivitySign() != null && !TextUtils.isEmpty(item.getActivitySign())) || (mClassifyType == 1 && item.getGoodsActivitySign() != null && !TextUtils.isEmpty(item.getGoodsActivitySign()))) {
            viewHolder.getView(R.id.rtv_activity_label).setVisibility(View.VISIBLE);
            viewHolder.setText(R.id.rtv_activity_label, mClassifyType == 0 ? item.getActivitySign() : item.getGoodsActivitySign());
        } else {
            viewHolder.getView(R.id.rtv_activity_label).setVisibility(View.GONE);
        }
    }


    public interface OnAddToCartClickListener {
        //        // 点击item
        //        void onItemClick(View v, RecyclerView.ViewHolder vh, int position);

        // 点击添加到购物车图片
        void onAddToCartClick(ImageView v, RecyclerView.ViewHolder vh, int position);
    }

    public void setOnAddToCartClickListener(OnAddToCartClickListener listener) {
        this.mOnAddToCartClickListener = listener;
    }
}
